package uy.com.tsi2G7.superbet.ui;

import java.rmi.RemoteException;
import java.util.Date;
import java.util.Map;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import uy.com.tsi2G7.superbet.account.InsufficientFoundException;
import uy.com.tsi2G7.superbet.account.dao.AccountNotFoundException;
import uy.com.tsi2G7.superbet.bet.Bet;
import uy.com.tsi2G7.superbet.bet.BetService;
import uy.com.tsi2G7.superbet.bet.SimpleBet;

@ManagedBean(name = "betBean")
@SessionScoped
public class BetBean extends AbstractBean{	
	@ManagedProperty(value="#{userBean}")
	private UserBean userBean;
	@EJB
	private BetService betService;
	@ManagedProperty(value="#{userAccountBean}")
	private UserAccountBean userAccountBean;	
	
	public BetBean() {
	}
	
	public String goToSimpleBetConfirmation(){
		loadEvents();
		return "simple-bet";
	}
	
	public String goToCompositeBetConfirmation(){
		loadEvents();
		return "composite-bet";
	}
	
	public String goToUserBetConfirmation(){
		loadEvents();
		return "user-bet";
	}
	
	private void loadEvents() {
		eventType=panelMenuBean.getCurrentGroup();
		market=panelMenuBean.getMarket();
		marketResult=panelMenuBean.getMarketResult();
		event=panelMenuBean.getCurrentSample();
		
	}

	public String cleanBet() {
		userAccountBean.cleanCompsoiteBet();
		return "welcome";
	}
	
	public String doBet() {
		try {
			betService.doSimpleBet(userBean.getMail()
					,eventType.getId()
					,event.getId()
					,market.getId()
					,marketResult.getId()
					,amount);
		} catch (RemoteException e) {
			this.showErrorMessage(e.getMessage());
			e.printStackTrace();
			return null;
		} catch (AccountNotFoundException e) {
			this.showErrorMessage("No se encuentra la cuenta");
			e.printStackTrace();
			return null;
		} catch (InsufficientFoundException e) {
			this.showErrorMessage("Balance en la cuenta insuficiente");
			e.printStackTrace();
			return null;
		}

		return "bet-success";
	}
	
	public String doUserBet() {
		try {
			betService.doUserBet(userBean.getMail()
					,eventType.getId()
					,event.getId()
					,market.getId()
					,marketResult.getId()
					,amount
					,true);
		} catch (RemoteException e) {
			this.showErrorMessage(e.getMessage());
			e.printStackTrace();
			return null;
		} catch (AccountNotFoundException e) {
			this.showErrorMessage("No se encuentra la cuenta");
			e.printStackTrace();
			return null;
		} catch (InsufficientFoundException e) {
			this.showErrorMessage("Balance en la cuenta insuficiente");
			e.printStackTrace();
			return null;
		}

		return "bet-success";
	}
	
	public String doUserBetConfirm() {
		try {
			//Se confirma la puesta del usuario original
			Map<String, String> requestMap = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
			FacesContext.getCurrentInstance().getExternalContext().getInitParameterMap();
			String result = (String) requestMap.get("ResultId");
			String markId = (String) requestMap.get("MarketId");
			String user = (String) requestMap.get("User");
			SimpleBet bet = (SimpleBet)betService.confirmUserBet(user,new Long(markId),new Long(result));
			//Se crea la nueva apuesta
			betService.doUserBet(userBean.getMail()
					,bet.getEventTypeId()
					,bet.getEventId()
					,bet.getMarketId()
					,bet.getResultId()
					,bet.getAmount()
					,false);
		} catch (RemoteException e) {
			this.showErrorMessage(e.getMessage());
			e.printStackTrace();
			return null;
		} catch (AccountNotFoundException e) {
			this.showErrorMessage("No se encuentra la cuenta");
			e.printStackTrace();
			return null;
		} catch (InsufficientFoundException e) {
			this.showErrorMessage("Balance en la cuenta insuficiente");
			e.printStackTrace();
			return null;
		}

		return "bet-success";
	}
	
	public String doCompositeBet() {
		userAccountBean.addBetToComposite(userBean.getMail()
				,eventType.getId()
				,event.getId()
				,market.getId()
				,marketResult.getId(),
				getEventTypeName()
				,getEventName()
				,getMarketName()
				,getMarketResultName());
		return "welcome";
	}

	public String finishCompositeBet() {
		try {
			if(userAccountBean.getCompositeBet()==null
					||userAccountBean.getCompositeBet().getBets()==null
					||userAccountBean.getCompositeBet().getBets().length==0){
				this.showErrorMessage("Debe ingresar resultados a la apuesta");
				return null;
			}
			Date date = new Date();
			userAccountBean.getCompositeBet().setUserId(userBean.getMail());
			userAccountBean.getCompositeBet().setAmount(getAmount());
			userAccountBean.getCompositeBet().setDate(date);
			for (SimpleBet bet : userAccountBean.getCompositeBet().getBets()) {
				bet.setAmount(getAmount());
				bet.setDate(date);
				bet.setComposite(true);
			}
				
			betService.doBet(userAccountBean.getCompositeBet());
			userAccountBean.cleanCompsoiteBet();
			
		} catch (RemoteException e) {
			this.showErrorMessage(e.getMessage());
			e.printStackTrace();
			return null;
		} catch (AccountNotFoundException e) {
			this.showErrorMessage("No se encuentra la cuenta");
			e.printStackTrace();
			return null;
		} catch (InsufficientFoundException e) {
			this.showErrorMessage("Balance en la cuenta insuficiente");
			e.printStackTrace();
			return null;
		}

		return "bet-success";

	}
	
	
	
	public UserBean getUserBean() {
		return userBean;
	}

	public void setUserBean(UserBean userBean) {
		this.userBean = userBean;
	}

	public BetService getBetService() {
		return betService;
	}

	public void setBetService(BetService betService) {
		this.betService = betService;
	}

	public UserAccountBean getUserAccountBean() {
		return userAccountBean;
	}

	public void setUserAccountBean(UserAccountBean userAccountBean) {
		this.userAccountBean = userAccountBean;
	}

	




	
	
	
	
}
